<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
    $this->display('index');
}

//获取验证码
public function get_code(){
   echo  getCode(100,25);
}
  
   //登录验证  
    public function checkLogin(){
      if($_POST){
           $userModel=D('Users');
           $map['user_name']=$_POST['username'];
           $user = $userModel->where("user_name = '".$_POST['username']."'")->field('salt')->find();
           if(empty($user['salt'])){
            $this->error('登陆失败!');
           }
           $num = $_POST['password'].$user['salt'];
           $password = sha1($num);
            $res= $userModel->where("user_pwd = '".$password."'")->find();
            
            if(isset($res)){
              $data['last_login'] = date('Y-m-d H:i:s');
              $data['last_ip'] = $_SERVER["REMOTE_ADDR"];
              $result = D('Users')->where('user_id='.$res['user_id'])->save($data);
              if($result){
                 session('users',$res);
                if($_POST['state'] ==2){
                 $this->redirect('User/index');
               }else{
                 $this->redirect('Index/index'); 
                }
              }
            }else{
              $this->error('用户名或密码错误!');
            }
      }else{
        $this->error('非法访问!');
      }
    } 
    
   //修改密码
    public function news_password(){
     $password =$_POST['password'];
     $password_confirmation =  $_POST['password_confirmation'];
    if($password !=$password_confirmation){
       $this->error('密码不一致!');
    }else{
      $userModel=D('Users');
      $data['password'] = sha1($password);
      $user = session('user');
      $map['id'] = $user['id'];
      $res = $userModel ->where($map)->save($data);
        if($res != flase){
           session_destroy(); 
           $this->redirect('index');
        }else{
           $this->error('非法操作!');
        }
    }

    }

    
    //退出登录
    public function logout(){
        session_destroy();  //清除服务器的sesion文件
        $this->redirect('Login/index');
    }

    //注册
    public function register(){
      extract($_POST);
      if(strlen(trim($re_username))<4 || strlen(trim($re_username>20))){
        echo json_encode(array('code'=>2,'msg'=>'用户名必须大于4位，小于20位'));exit;
      }
      if(strlen(trim($re_password))<4|| strlen(trim($re_password))>16){
        echo json_encode(array('code'=>2,'msg'=>'密码必须大于4位，小于16位'));exit;
      }
     if(strlen($re_old_password)<4||strlen($re_old_password)>16){
       echo json_encode(array('code'=>2,'msg'=>'密码必须大于4位，小于16位'));exit;
     }
     if(trim($re_password) !==trim($re_old_password)){
      echo json_encode(array('code'=>2,'msg'=>'密码不一致'));exit;
     }
     $userlist = D('Users')->where("user_name='".$re_username."'")->find();
     if($userlist){
      echo json_encode(array('code'=>2,'msg'=>'该用户名以存在!'));exit;
     }

     //验证验证码
     $_code = $_SESSION['code'];
     if($re_code != $_code){
      echo json_encode(array('code'=>2,'msg'=>'验证码不正确!'));exit;
     }
     //注册用户
     $email = empty($re_email)?"":$re_email;
     $salt = $this->random(4,1);
     $password = sha1($re_password.$salt);
     $data['user_name'] = $re_username;
     $data['user_pwd'] = $password;
     $data['salt']=$salt;
     $data['email'] = $email;
     $data['head'] = 'img/touxiang.jpg';
     $data['createtime'] = date('Y-m-d H:i:s');
     $data['last_ip'] =$_SERVER["REMOTE_ADDR"];
     $res = D('Users')->add($data);
     if($res){
       echo json_encode(array('code'=>1,'msg'=>'注册成功!','username'=>$re_username,'password'=>$re_password));exit;
     }else{
       echo json_encode(array('code'=>2,'msg'=>'注册失败，请稍后再试!'));exit;
     }

    }



  function random($length = 6 , $numeric = 0) {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
  }
}
?>